/*
 * @filename CORSConfiguration.java
 * @author barry
 * @version 0.0.1
 * @date 2020年3月13日
 */
package com.bnzj.core.webflux.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.reactive.config.CorsRegistry;
import org.springframework.web.reactive.config.WebFluxConfigurer;

/**
 * 跨域配置
 * @author barry
 * @date 2020-03-13
 */
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
@Configuration(proxyBeanMethods = false)
public class CORSConfiguration {

    @ConditionalOnProperty(prefix = "bnzj.cors", name = "enabled", havingValue = "true", matchIfMissing = true)
    @ConditionalOnMissingBean(CorsFilter.class)
    @Bean
    public WebFluxConfigurer corsConfigurer() {
        return new WebFluxConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**").allowCredentials(true).allowedOrigins("*").allowedHeaders("*").allowedMethods("*");
            }
        };
    }
}
